package com.jiayang.hhotel.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.adapter.FinanceHandleListAdapter;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.bean.FinanceHandleModel;
import com.jiayang.hhotel.presenter.FinanceHandlePresenter;
import com.jiayang.hhotel.util.CommonUtil;
import com.jiayang.hhotel.util.LogUtil;
import com.jiayang.hhotel.util.StringUtil;
import com.jiayang.hhotel.util.ToastUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;

import java.util.List;

import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/25 0025.
 * 财务处理页面
 */

public class FinanceHandleActivity extends XActivity<FinanceHandlePresenter> implements FinanceHandleListAdapter.btClickListener {

    private View layout;
    private ImageView icon;
    private TextView tv;

    private SwipeRefreshLayout fresh;
    private SwipeMenuRecyclerView recyclerView;

    private FinanceHandleListAdapter adapter;

    private int page =1;

    @Override
    public void initData(Bundle savedInstanceState) {
        initTitle(this,true,getString(R.string.finance_handle),R.color.orange_color,null,null);

        fresh= (SwipeRefreshLayout) findViewById(R.id.fresh);
        recyclerView= (SwipeMenuRecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        layout = findViewById(R.id.ll_data);
        icon = (ImageView) findViewById(R.id.iv_pic);
        tv = (TextView) findViewById(R.id.tv_text);

        isLoadMore = true;
        isFresh(true);
        getP().asyncListLoad(Constant.LAUGUAGE_FLAG_CHINESE);
        fresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if(!isLoadMore){
                    page = 1;
                    getP().asyncListLoad(Constant.LAUGUAGE_FLAG_CHINESE);
                }else{
                    isFresh(false);
                }
            }
        });

        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                if (!recyclerView.canScrollVertically(1)) {// 手指不能向上滑动了
                    if (!isLoadMore) {
                        isLoadMore = true;
                        page++;
                        getP().asyncListLoad(Constant.LAUGUAGE_FLAG_CHINESE);
                    }
                }
            }
        });
    }

    public void setAdapter(List<FinanceHandleModel.PCashierListDtosBean> list){
        isFresh(false);
        isLoadMore = false;
        if(adapter == null){
            adapter = new FinanceHandleListAdapter(this,list,this);
            recyclerView.setAdapter(adapter);
        }else{
            adapter.notifyDataSetChanged();
        }
    }

    private void isFresh(final boolean is){
        fresh.post(new Runnable() {
            @Override
            public void run() {
                fresh.setRefreshing(is);
            }
        });
    }
    @Override
    public int getLayoutId() {
        return R.layout.layout_finance_handle;
    }

    @Override
    public FinanceHandlePresenter newP() {
        return new FinanceHandlePresenter();
    }

    @Override
    public void onBTClickListener(FinanceHandleModel.PCashierListDtosBean model, int id, int position) {
        Intent intent;
        Bundle bundle;
        switch (id){
            case R.id.bt_per_person_rounds://个人查房
                CommonUtil.showProgressDialog(FinanceHandleActivity.this,R.string.loading);
                getP().asyncPerOrTeamLoad(Constant.LAUGUAGE_FLAG_CHINESE,0,model.getRoom());
                break;
            case R.id.bt_team_person_rounds://团队查房
                CommonUtil.showProgressDialog(FinanceHandleActivity.this,R.string.loading);
                getP().asyncPerOrTeamLoad(Constant.LAUGUAGE_FLAG_CHINESE,1,model.getRoom());
                break;
            case R.id.bt_check_out://结账退房
                intent = new Intent(FinanceHandleActivity.this,CheckOutActivity.class);
                bundle = new Bundle();
                bundle.putSerializable(Constant.INTENT_PROJECT_KEY,model);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
            case R.id.bt_audit://查账
                intent = new Intent(FinanceHandleActivity.this,AuditActivity.class);
                bundle = new Bundle();
                bundle.putSerializable(Constant.INTENT_PROJECT_KEY,model);
                intent.putExtras(bundle);
                startActivity(intent);
                break;
        }
    }

    private boolean isLoadMore;//判断是否正在加载。用于防止加载更多时重复加载
    /**
     * 请求错误
     * @param state 状态
     */
    public void sealPic(final int state){
        LogUtil.showLog("state="+state);
        isFresh(false);
        isLoadMore = false;
        if(page == 1){//只有第一次加载不出数据时才会显示占位图
            fresh.setVisibility(View.GONE);
            layout.setVisibility(View.VISIBLE);
            switch (state){
                case 1002://没有数据
                    icon.setImageResource(Constant.NO_DATA_PICTURE);
                    tv.setText(getString(R.string.no_data));
                    icon.setOnClickListener(null);
                    break;
                case -1://服务器错误
                    icon.setImageResource(Constant.NET_ERROR_PICTURE);
                    tv.setText(getString(R.string.service_error_contract_manager));
                    icon.setOnClickListener(null);
                    break;
                default://其他错误
                    icon.setImageResource(Constant.TIME_OUT_PICTURE);
                    tv.setText(getString(R.string.service_error));
                    icon.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            fresh.setVisibility(View.VISIBLE);
                            layout.setVisibility(View.GONE);
                            isFresh(true);
                            getP().asyncListLoad(Constant.LAUGUAGE_FLAG_CHINESE);
                        }
                    });
                    break;
            }
        }else{
            ToastUtil.Toast(StringUtil.errMsg(state),this);
        }
    }
}
